package com.av.android;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.databind.ObjectMapper;

public class LoginActivity extends BaseActivity {

	public static User user;
	private String username;
	private String password;
	private Button loginButton;
	private Button guestButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginButton = (Button) findViewById(R.id.but_login);
        guestButton = (Button) findViewById(R.id.but_guest);
        
        loginButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				username = ((EditText)findViewById(R.id.username)).getText().toString();
				password = ((EditText)findViewById(R.id.password)).getText().toString();
				showLoading("Logging in...");
				new LoginTask().execute();
				
			}
		});
        guestButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(), GuestActivity.class));
				
			}
		});
        
    }

    private class LoginTask extends AsyncTask<Void, Void, Boolean>{

		@Override
		protected Boolean doInBackground(Void... params) {
			String responseString = null; 
			try{
				HttpClient httpclient = new DefaultHttpClient();
			    HttpResponse response = httpclient.execute(new HttpGet("http://titan.cmpe.boun.edu.tr:8085/cmpe451Web/AndroidLogin?user="+username+"&pass="+password));
			    StatusLine statusLine = response.getStatusLine();
			    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
			        ByteArrayOutputStream out = new ByteArrayOutputStream();
			        response.getEntity().writeTo(out);
			        out.close();
			        responseString = out.toString();
			        //..more logic
			    } else{
			        //Closes the connection.
			        response.getEntity().getContent().close();
			        throw new IOException(statusLine.getReasonPhrase());
			    }
			}catch(Exception e){
				Log.e("",e.toString());
			}
			if(responseString==null || responseString.equals("invalid"))
				return false;
			ObjectMapper mapper = new ObjectMapper();
			try {
				user = mapper.readValue(responseString, User.class);
				if(user.getUser_id()!=-1)
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return false;
		}
    	@Override
    	protected void onPostExecute(Boolean loggedin){
    		hideLoading();
    		if(!loggedin){
    			alertWithOnClickHandler("Wrong username or password, Try again!", "Warning", new Handler());
    		}else{
    			startActivity(new Intent(getApplicationContext(), MemberOptionsActivity.class));
    		}
    		
    	}
    }
}
